#include "laurin-network.h"
#include "laurin-utils.h"


Laurin80211Network::Laurin80211Network(const Glib::ustring& ssid)
{
	_ssid = Glib::ustring(ssid);
}


Laurin80211Network::~Laurin80211Network()
{
	_accessPoints.clear();
	_stations.clear();
}


Glib::ustring Laurin80211Network::getNetworkSSID() const
{
	return _ssid;
}


void Laurin80211Network::addAccessPoint(const LaurinAccessPoint& accessPoint, bool checkPresent)
{
	if(checkPresent && isAccessPointPresent(accessPoint.getBSSID()))
		return;

	_accessPoints.push_front(accessPoint);
}


void Laurin80211Network::addStation(const Laurin80211Station& station, bool checkPresent)
{
	if(checkPresent && isStationPresent(station.getMAC()))
		return;

	_stations.push_front(station);
}


bool Laurin80211Network::isAccessPointPresent(u_int8_t* bssid)
{
	std::list<LaurinAccessPoint>::iterator b = _accessPoints.begin();
	std::list<LaurinAccessPoint>::iterator e = _accessPoints.end();

	while(b != e)
	{
		if(areMACEquals((*b).getBSSID(), bssid))
			return true;

		b++;
	}

	return false;

}


bool Laurin80211Network::isStationPresent(u_int8_t* mac)
{
	std::list<Laurin80211Station>::iterator b = _stations.begin();
	std::list<Laurin80211Station>::iterator e = _stations.end();

	while(b != e)
	{
		if(areMACEquals((*b).getMAC(), mac))
			return true;

		b++;
	}

	return false;
}


const std::list<LaurinAccessPoint>& Laurin80211Network::getAccessPoint() const
{
	return _accessPoints;
}


const std::list<Laurin80211Station>& Laurin80211Network::getStations() const
{
	return _stations;
}


int Laurin80211Network::getNumberOfAccessPoint() const
{
	return _accessPoints.size();
}


int Laurin80211Network::getNumberOfStations() const
{
	return _stations.size();
}


/*
short Laurin80211Network::isMACPresent(u_int8_t* op1, u_int8_t* op2)
{
	std::list<Laurin80211Station>::iterator b = _stations.begin();
	std::list<Laurin80211Station>::iterator e = _stations.end();

	while(b != e)
	{
		if(areMACEquals((*b).getMAC(), op1))
			return 0;
		else if(areMACEquals((*b).getMAC(), op2))
			return 1;
		b++;
	}

	return -1;
}
*/



//-------------------------------- NETWORK SET ------------------- //


LaurinNetworkSet::LaurinNetworkSet(){}


LaurinNetworkSet::~LaurinNetworkSet()
{
	_networks.clear();
}


Laurin80211Network* LaurinNetworkSet::searchNetworkFromSSID(const Glib::ustring& ssid)
{
	std::list<Laurin80211Network>::iterator b = _networks.begin();
	std::list<Laurin80211Network>::iterator e = _networks.end();

	while(b != e)
	{
		if(ssid == (*b).getNetworkSSID())
			return &(*b);

		b++;
	}

	return NULL;
}


Laurin80211Network* LaurinNetworkSet::searchNetworkFromBSSID(u_int8_t* bssid)
{
	std::list<Laurin80211Network>::iterator b = _networks.begin();
	std::list<Laurin80211Network>::iterator e = _networks.end();

	while(b != e)
	{
		if((*b).isAccessPointPresent(bssid))
			return &(*b);

		b++;
	}

	return NULL;
}


Laurin80211Network* LaurinNetworkSet::addNetwork(const Glib::ustring& ssid, bool checkPresent)
{
	if(checkPresent && (searchNetworkFromSSID(ssid) != NULL))
		return NULL;

	_networks.push_front(Laurin80211Network(ssid));
	return &(*(_networks.begin()));
}


int LaurinNetworkSet::getNumberOfNetworks() const
{
	return _networks.size();
}


const std::list<Laurin80211Network>& LaurinNetworkSet::getNetworks() const
{
	return _networks;
}
